package com.itheima.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.goods.mapper.BrandMapper;
import com.itheima.goods.pojo.Brand;
import com.itheima.goods.query.QueryBrand;
import com.itheima.goods.service.BrandService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 继承 ServiceImpl 目的：父类中已经帮助我们实现了CRUD单表的基本操作
 * @Version: V1.0
 */
@Service  // ****
public class BrandServiceimpl extends ServiceImpl<BrandMapper, Brand>
        implements BrandService {


    @Override
    public Result findPage(QueryBrand queryBrand, Integer page, Integer size) {
        // 构建查询条件
        LambdaQueryWrapper<Brand> wrapper = Wrappers.lambdaQuery();

        // 判断条件是否存在
        if (queryBrand.getName() != null && queryBrand.getName().length() > 0) {
            wrapper.like(Brand::getName, queryBrand.getName());
        }

        if (queryBrand.getLetter() != null && queryBrand.getLetter().length() > 0) {
            wrapper.eq(Brand::getLetter, queryBrand.getLetter().toUpperCase()); // 大小写
        }

        // page
        if (page <= 0) {
            page = 1;
        }
        if (size <= 0) {
            size = 10;
        }

        // 分页查询
        IPage<Brand> ipage = new Page<>(page, size);
        IPage<Brand> brandIPage = page(ipage, wrapper);

        // 返回分页对象
        PageResult pageResult = new PageResult(brandIPage.getTotal(), brandIPage.getRecords());

        return new Result(true, StatusCode.OK, "查询成功",pageResult);
    }

    @Override
    public Result findList(QueryBrand queryBrand) {
        // 构建查询条件
        LambdaQueryWrapper<Brand> wrapper = Wrappers.lambdaQuery();

        // 判断条件是否存在
        if (queryBrand.getName() != null && queryBrand.getName().length() > 0) {
            wrapper.like(Brand::getName, queryBrand.getName());
        }

        if (queryBrand.getLetter() != null && queryBrand.getLetter().length() > 0) {
            wrapper.eq(Brand::getLetter, queryBrand.getLetter().toUpperCase()); // 大小写
        }

        // 执行查询
        List<Brand> brandList = list(wrapper);

        // 返回结果
       return new Result(true, StatusCode.OK, "查询成功",brandList);
    }

    /**
     * 新增
     * @param brand
     * @return
     */
    @Override
    public Result add(Brand brand) {

        // 首字母转成大写
        if (brand.getLetter() != null && brand.getLetter().length() > 0) {
            brand.setLetter( brand.getLetter().toUpperCase() );
        }

        boolean save = save(brand);// 父类

        return (save ? new Result(true, StatusCode.OK, "新增成功")
                : new Result(false, StatusCode.ERROR, "新增失败"));
    }

    @Override
    public Result findById(Integer id) {
        Brand brand = getById(id);
        return new Result(true, StatusCode.OK, "查询成功",brand);
    }

    @Override
    public Result updateById(Brand brand, Integer id) {
        Brand one = getById(id);

        if (one == null) {
            System.out.println("id不存在");
            return new Result(false, StatusCode.ERROR, "ID不存在");
        }

        // 首字母转成大写
        if (brand.getLetter() != null && brand.getLetter().length() > 0) {
            brand.setLetter( brand.getLetter().toUpperCase() );
        }
        brand.setId(id);

        updateById(brand);

        return new Result(true, StatusCode.OK, "修改成功");
    }

    @Override
    public Result deleteById(Integer id) {
        removeById(id);
        return new Result(true, StatusCode.OK, "删除成功");
    }
}
